package com.example.sbeducation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sbeducation.common.Result;
import com.example.sbeducation.dto.HomeworkGradeDTO;
import com.example.sbeducation.dto.HomeworkSubmissionDTO;
import com.example.sbeducation.entity.HomeworkSubmission;
import com.example.sbeducation.entity.User;
import com.example.sbeducation.service.HomeworkSubmissionService;
import com.example.sbeducation.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 作业提交控制器
 */
@RestController
@RequestMapping("/api/homework/submission")
public class HomeworkSubmissionController {

  @Autowired
  private HomeworkSubmissionService homeworkSubmissionService;

  @Autowired
  private UserService userService;

  /**
   * 提交作业
   */
  @PostMapping
  public Result<Long> submitHomework(@Valid @RequestBody HomeworkSubmissionDTO homeworkSubmissionDTO) {
    Long submissionId = homeworkSubmissionService.submitHomework(homeworkSubmissionDTO);
    return Result.success(submissionId);
  }

  /**
   * 批改作业
   */
  @PutMapping("/grade")
  public Result<Void> gradeHomework(@Valid @RequestBody HomeworkGradeDTO homeworkGradeDTO) {
    homeworkSubmissionService.gradeHomework(homeworkGradeDTO);
    return Result.success("批改成功");
  }

  /**
   * 获取作业提交详情
   */
  @GetMapping("/{submissionId}")
  public Result<HomeworkSubmissionDTO> getSubmissionDetail(@PathVariable Long submissionId) {
    HomeworkSubmission submission = homeworkSubmissionService.getSubmissionById(submissionId);

    // 转换为DTO，确保前端可以获取到所有需要的信息
    HomeworkSubmissionDTO dto = new HomeworkSubmissionDTO();
    BeanUtils.copyProperties(submission, dto);

    // 确保attachmentUrl被正确设置
    if (submission.getAttachmentUrl() != null && !submission.getAttachmentUrl().isEmpty()) {
      dto.setAttachmentUrl(submission.getAttachmentUrl());
    }

    // 如果使用feedback字段而非comment字段
    if (submission.getComment() != null) {
      dto.setFeedback(submission.getComment());
    }

    // 获取学生信息
    try {
      User student = userService.getUserById(submission.getStudentId());
      if (student != null) {
        dto.setStudentName(student.getRealName());
      }
    } catch (Exception e) {
      // 忽略获取学生信息失败的情况
    }

    return Result.success(dto);
  }

  /**
   * 获取作业的提交列表
   */
  @GetMapping("/homework/{homeworkId}")
  public Result<Page<HomeworkSubmission>> getHomeworkSubmissions(
      @PathVariable Long homeworkId,
      @RequestParam(defaultValue = "1") Integer page,
      @RequestParam(defaultValue = "10") Integer size) {
    Page<HomeworkSubmission> submissions = homeworkSubmissionService.getSubmissionsByHomeworkId(homeworkId, page, size);
    return Result.success(submissions);
  }

  /**
   * 获取学生的作业提交列表
   */
  @GetMapping("/student/{studentId}")
  public Result<Page<HomeworkSubmission>> getStudentSubmissions(
      @PathVariable Long studentId,
      @RequestParam(defaultValue = "1") Integer page,
      @RequestParam(defaultValue = "10") Integer size) {
    Page<HomeworkSubmission> submissions = homeworkSubmissionService.getSubmissionsByStudentId(studentId, page, size);
    return Result.success(submissions);
  }

  /**
   * 获取学生特定作业的提交
   */
  @GetMapping("/student/{studentId}/homework/{homeworkId}")
  public Result<HomeworkSubmissionDTO> getStudentHomeworkSubmission(
      @PathVariable Long studentId,
      @PathVariable Long homeworkId) {
    HomeworkSubmission submission = homeworkSubmissionService.getStudentSubmission(homeworkId, studentId);

    if (submission == null) {
      return Result.success(null);
    }

    // 转换为DTO
    HomeworkSubmissionDTO dto = new HomeworkSubmissionDTO();
    BeanUtils.copyProperties(submission, dto);

    // 确保attachmentUrl被正确设置
    if (submission.getAttachmentUrl() != null && !submission.getAttachmentUrl().isEmpty()) {
      dto.setAttachmentUrl(submission.getAttachmentUrl());
    }

    // 如果使用feedback字段而非comment字段
    if (submission.getComment() != null) {
      dto.setFeedback(submission.getComment());
    }

    // 获取学生信息
    try {
      User student = userService.getUserById(submission.getStudentId());
      if (student != null) {
        dto.setStudentName(student.getRealName());
      }
    } catch (Exception e) {
      // 忽略获取学生信息失败的情况
    }

    return Result.success(dto);
  }
}